package com.alibaba.cirtus.ots.application.adapter.ability.utils;

import com.epoch.app.otsapplicationadaptersdk.model.dto.MnNykForecastOrderLog;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnNykForecastOrderLogResponse;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnNykForecastOrderNumLog;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

public class MnNykForecastOrderLogUtil {

    private static MnNykForecastOrderLogResponse mnNykForecastOrderLogs = new MnNykForecastOrderLogResponse();

    public static MnNykForecastOrderLogResponse getAll(){
        return mnNykForecastOrderLogs;
    }

    public static List<MnNykForecastOrderLog> getSuccessListByFromSys(String fromSys){
        List<MnNykForecastOrderLog> list = Lists.newArrayList();
        List<MnNykForecastOrderLog> success = mnNykForecastOrderLogs.getSuccessList();
        if(CollectionUtils.isEmpty(success)){
            return null;
        }
        for (MnNykForecastOrderLog mnNykForecastOrderLog : success) {
            if(fromSys.equals(mnNykForecastOrderLog.getFromSys())){
                list.add(mnNykForecastOrderLog);
            }
        }
        return list;
    }

    public static List<MnNykForecastOrderLog> getFailListByFromSys(String fromSys){
        List<MnNykForecastOrderLog> list = Lists.newArrayList();
        if (StringUtils.isEmpty(fromSys)){
            return list;
        }
        List<MnNykForecastOrderLog> fail = mnNykForecastOrderLogs.getFailList();
        if(CollectionUtils.isEmpty(fail)){
            return null;
        }
        for (MnNykForecastOrderLog mnNykForecastOrderLog : fail) {
            if(fromSys.equals(mnNykForecastOrderLog.getFromSys())){
                list.add(mnNykForecastOrderLog);
            }
        }
        return list;
    }

    public static List<MnNykForecastOrderNumLog> getNum(String fromSys){
        List<MnNykForecastOrderNumLog> list = Lists.newArrayList();
        List<MnNykForecastOrderNumLog> nums = mnNykForecastOrderLogs.getNum();
        if(CollectionUtils.isEmpty(nums)){
            return null;
        }
        for (MnNykForecastOrderNumLog num : nums) {
            if(fromSys.equals(num.getFromSys())){
                list.add(num);
            }
        }
        return list;
    }

    public static void addSuccess(MnNykForecastOrderLog MnNykForecastOrderLog){
        if(Objects.nonNull(MnNykForecastOrderLog)){
            List<MnNykForecastOrderLog> list =
                    mnNykForecastOrderLogs.getSuccessList();
            if(CollectionUtils.isEmpty(list)){
                List<MnNykForecastOrderLog> l = Lists.newArrayList();
                l.add(MnNykForecastOrderLog);
                mnNykForecastOrderLogs.setSuccessList(l);
                return;
            }
            list.add(MnNykForecastOrderLog);
        }
    }

    public static void addFail(MnNykForecastOrderLog MnNykForecastOrderLog){
        if(Objects.nonNull(MnNykForecastOrderLog)){
            List<MnNykForecastOrderLog> list =
                    mnNykForecastOrderLogs.getFailList();
            if(CollectionUtils.isEmpty(list)){
                List<MnNykForecastOrderLog> l = Lists.newArrayList();
                l.add(MnNykForecastOrderLog);
                mnNykForecastOrderLogs.setFailList(l);
                return;
            }
            list.add(MnNykForecastOrderLog);
        }
    }

    public static void setNum(AtomicInteger successNum, AtomicInteger failNum, String fromSys){
        List<MnNykForecastOrderNumLog> nums =
                mnNykForecastOrderLogs.getNum();
        if(CollectionUtils.isNotEmpty(nums)){
            //不为空循环找对应的数据
            for(int i = 0;i < nums.size();i++){
                String fs = nums.get(i).getFromSys();
                if(StringUtils.isBlank(fs)){
                    continue;
                }
                if(!fromSys.equals(nums.get(i).getFromSys())){
                    continue;
                }
                Integer fail = nums.get(i).getFailNum();
                Integer success = nums.get(i).getSuccessNum();
                nums.get(i).setFailNum(fail.intValue() + failNum.intValue());
                nums.get(i).setSuccessNum(success.intValue() + successNum.intValue());
                return;
            }
        }
        MnNykForecastOrderNumLog num = new MnNykForecastOrderNumLog();
        num.setFailNum(failNum.intValue());
        num.setSuccessNum(successNum.intValue());
        num.setFromSys(fromSys);
        List<MnNykForecastOrderNumLog> newNums = Lists.newArrayList();
        newNums.add(num);
        mnNykForecastOrderLogs.setNum(newNums);
    }


    public static void removeAll(String fromSys){
        List<MnNykForecastOrderLog> list =
                Lists.newArrayList();
        mnNykForecastOrderLogs.setSuccessList(list);
        mnNykForecastOrderLogs.setFailList(list);
        List<MnNykForecastOrderNumLog> nums =
                Lists.newArrayList();
        mnNykForecastOrderLogs.setNum(nums);
    }

    public static void removeByFromSys(String fromSys){
        List<MnNykForecastOrderLog> failList =
                mnNykForecastOrderLogs.getFailList();
        if(CollectionUtils.isEmpty(failList)){
            return;
        }
        Iterator<MnNykForecastOrderLog> itFail = failList.iterator();
        while(itFail.hasNext()){
            MnNykForecastOrderLog fail = itFail.next();
            if(fromSys.equals(fail.getFromSys())){
                itFail.remove();
            }
        }

        List<MnNykForecastOrderLog> successList =
                mnNykForecastOrderLogs.getSuccessList();
        if(CollectionUtils.isEmpty(successList)){
            return;
        }
        Iterator<MnNykForecastOrderLog> itSuccess = successList.iterator();
        while(itSuccess.hasNext()){
            MnNykForecastOrderLog success = itSuccess.next();
            if(fromSys.equals(success.getFromSys())){
                itSuccess.remove();
            }
        }

        List<MnNykForecastOrderNumLog> nums =
                mnNykForecastOrderLogs.getNum();
        Iterator<MnNykForecastOrderNumLog> itNums = nums.iterator();
        while(itNums.hasNext()){
            MnNykForecastOrderNumLog num = itNums.next();
            if(fromSys.equals(num.getFromSys())){
                itNums.remove();
            }
        }

    }



}
